/*******************************************************************************
 * Copyright (c) 2010, 2017 西安秦晔信息科技有限公司
 * Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 *******************************************************************************/
package com.qinyeit.webapp.controller.management;

import com.alibaba.fastjson.JSON;
import com.qinyeit.serviceapp.entity.District;
import com.qinyeit.serviceapp.service.organization.DistrictService;
import com.qinyeit.webapp.utils.CurrentAccountUtils;
import com.qinyetech.springstage.core.web.controller.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * <p>ClassName: com.qinyetech.webapp.web.controller.management.system.BaseManagementController
 * <p>Function: 后台管理Controller基础类
 * <p>date: 2018-03-03 14:59
 *
 * @author wuqing
 * @version 1.0
 * @since JDK 1.8
 */
public abstract class BaseManagementController extends BaseController {

   @Autowired
    private DistrictService districtService;

   public List<District> getDistricts(){
       String dataPermissions = CurrentAccountUtils.getAuthenticatedManageBranch();
       if (CurrentAccountUtils.getAuthenticatedIsAdmin()){
           return districtService.getBuildTree(CurrentAccountUtils.getAuthenticatedMerchantGroupID(),true);
       }else {
           List<Long> branchIds = null;
           if (StringUtils.isNotBlank(dataPermissions)){
               branchIds = JSON.parseArray(dataPermissions,Long.class);
           }
           return districtService.getBuildTree(CurrentAccountUtils.getAuthenticatedMerchantGroupID(),true,branchIds,true);
       }
   }
}
